# This file is part of Acacia+, a tool for synthesis of reactive systems using antichain-based techniques
# Copyright (C) 2011-2013 UMONS-ULB
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import optparse

parser = optparse.OptionParser("")
parser.add_option("-i", "--input", dest="input_file", default="", type="string", help="Input file")
parser.add_option("-o", "--output", dest="output_file", default="", type="string", help="Output file")
parser.add_option("-f", "--format", dest="format", default="XLS", type="string", help="FORMAT (XLS or TEX)")


(options, args) = parser.parse_args()

try:
    o = open(options.input_file, 'r')
except IOError:
    print "Input file not found!"
    exit(0)
    
results = dict()
l = o.readline()
headers = ["id", "Real", "AC", "|A|", "Syn", "|M|", "Extr", "Total"]
while l != "":
    if l.startswith("Benchmark"):
        i=0
        cur_bench = l.split(": ")[1].replace("\n", "")
        results[cur_bench] = [headers]
    elif "Execution recap" in l:
        i+=1
        cur_result = [i]
        cur_automata_size = 0
        l = o.readline()
        if "unrealizable" in l:
            cur_result.append("False")
        elif "realizable" in l:
            cur_result.append("True")
        else:
            cur_result.append("?")
    elif l.startswith("Automata construction time"):
        cur_result.append(l.split(": ")[1].replace("\n", "").replace("s", ""))
    elif l.startswith("Number of states of automaton"):
        cur_automata_size += int(l.split(": ")[1].replace("\n", ""))
    elif l.startswith("Synthesis time"):
        cur_result.append(cur_automata_size)
        cur_result.append(l.split(": ")[1].replace("\n", "").replace("s", ""))
    elif l.startswith("Number of states of transition system"):
        cur_result.append(l.split(": ")[1].replace("\n", "").replace("s", ""))
    elif l.startswith("Solution(s) extraction time"):
        cur_result.append(l.split(": ")[1].replace("\n", "").replace("s", ""))
    elif l.startswith("Total time"):
        cur_result.append(l.split(": ")[1].replace("\n", "").replace("s", ""))
        results[cur_bench].append(cur_result)
    l = o.readline()

if options.format.lower() == "xls":
    to_print = ""
    for benchmark in results:
        to_print += benchmark
        for ex in results[benchmark]:
            for i in ex:
                to_print += str(i) + " "
            to_print += "\n"
        to_print += "\n"
elif options.format.lower() == "tex":
    to_print = "\\documentclass{article}\n"
    to_print += "\\usepackage{tabularx}\n"
    to_print += "\\begin{document}\n"
    to_print += "\\begin{center}\\large{"+options.input_file.replace("_", "-")+"}\\end{center}\n"
    for benchmark in results:
        to_print += "\\begin{table}[h!]\n"
        to_print += "\\caption{"+benchmark+"}\n"
        to_print += "\\begin{center}\n"
        to_print += "\\begin{tabular}{"
        for i in range(len(results[benchmark][0])):
            to_print += "|c"
        to_print += "|}\n\\hline\n"
        for i in range(len(results[benchmark][0])):
            to_print += "$"+results[benchmark][0][i]+"$"
            if i < len(results[benchmark][0])-1:
                to_print += " & "
            else:
                to_print += " \\\\\n"
        to_print += "\\hline\n"
        for i in range(1, len(results[benchmark])):
            for j in range(len(results[benchmark][i])):
                to_print += "$" + str(results[benchmark][i][j]) + "$"
                if j < len(results[benchmark][i])-1:
                    to_print += " & "
                else:
                    to_print += " \\\\\n"
            to_print += "\n"
        to_print += "\\hline\n\\end{tabular}\n\\end{center}\\end{table}"
    to_print += "\\end{document}"
else:
    print "Unknown output format!"
    exit(0)
    
try:
    output = open(options.output_file, "w")              
    output.write(to_print)
    output.close()
except IOError:
    print "Output file omitted!"
    exit(0)